﻿using OrkutAPILibrary.Constants;
using OrkutAPILibrary.Entities;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Friend Factory
    /// </summary>
    static class OrkutFriendFactory
    {
        /// <summary>
        /// Gets the self friends.
        /// </summary>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutPerson> GetSelfFriends(int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfFriends)
        {
            var extraData = new Dictionary<string, string>
			{
				{ "method", "people.get" },
				{ "userId", "@me" },
				{ "groupId", "@friends" },
                { "count", count.ToString()},
                { "startIndex", startIndex.ToString()}
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutData<OrkutPerson>>(requestResponse);
        }

        /// <summary>
        /// Gets the friends of user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutPerson> GetFriendsOfUser(string userId, int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfFriends)
        {
            var extraData = new Dictionary<string, string>
			{
				{ "method", "people.get" },
				{ "userId", userId },
				{ "groupId", "@friends" },
                { "count", count.ToString()},
                { "startIndex", startIndex.ToString()}
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutData<OrkutPerson>>(requestResponse);
        }

        /// <summary>
        /// Sends the friend request.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        internal static bool SendFriendRequest(string userId)
        {
            var extraData = new Dictionary<string, string>
								{
									{"method", "people.sendFriendRequest"},
									{"userId", "@me"},
									{"groupId", "@friends"},
									{"personId", userId},
								};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Accepts the friend request.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        internal static bool AcceptFriendRequest(string userId)
        {
            var extraData = new Dictionary<string, string>
								{
									{"method", "people.acceptFriendRequest"},
									{"userId", "@me"},
									{"groupId", "@friends"},
									{"personId", userId},
								};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Rejects the friend request.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        internal static bool RejectFriendRequest(string userId)
        {
            var extraData = new Dictionary<string, string>
								{
									{"method", "people.rejectFriendRequest"},
									{"userId", "@me"},
									{"groupId", "@friends"},
									{"personId", userId},
								};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Removes the friend request.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        internal static bool RemoveFriendRequest(string userId)
        {
            var extraData = new Dictionary<string, string>
								{
									{"method", "people.revokeFriendRequest"},
									{"userId", "@me"},
									{"groupId", "@friends"},
									{"personId", userId},
								};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Gets the birthday notifications.
        /// </summary>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutBirthdayNotification> GetBirthdayNotifications(int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfBirthdayNotifications)
        {
            var extraData = new Dictionary<string, string>
								{
									{"method", "messages.get"},
									{"userId", "@me"},
									{"groupId", "@friends"},
                                    { "messageType", "notification"},
				                    { "messageGroup", "birthdays"},
                                    { "count", count.ToString()},
                                    { "startIndex", startIndex.ToString()}
								};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutData<OrkutBirthdayNotification>>(requestResponse);
        }
    }
}
